package array

import (
	"sort"
)

func TopKFrequent(nums []int, k int) []int {

	freMap := map[int]int{}
	set := []int{}
	for _, v := range nums {
		if _, ok := freMap[v]; ok {
			freMap[v]++
		} else {
			freMap[v] = 1
			set = append(set, v)
		}
	}

	sort.Slice(set, func(i, j int) bool {
		return freMap[set[i]] > freMap[set[j]]
	})

	return set[0:k]

}
